<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      ref="queryFormRef"
      :inline="true"
      :model="queryParams"
      class="-mb-15px"
      label-width="68px"
    >
      <el-form-item label="版本名称" prop="versionName">
        <el-input
          v-model="queryParams.versionName"
          placeholder="请输入版本名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon class="mr-5px" icon="ep:search" />
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon class="mr-5px" icon="ep:refresh" />
          重置
        </el-button>
        <el-button plain type="primary" @click="updateForm('create')" v-if="addPermission">
          <Icon class="mr-5px" icon="ep:plus" />
          新增
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :show-overflow-tooltip="true">
      <el-table-column align="center" label="序号" type="index" width="80px"/>
      <el-table-column align="center" label="版本名称" prop="versionName" />
      <el-table-column align="center" label="版本描述" prop="versionDesc" />
      <el-table-column align="center" label="创建人" prop="creatorName" />
      <el-table-column align="center" label="创建时间" prop="createTime" />
      <el-table-column align="center" label="发布时间" prop="releaseTime" />
      <el-table-column align="center" label="状态" prop="status" />
      <el-table-column :width="300" align="center" label="操作" fixed="right" >
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="updateForm('update', scope.row)"
            v-if="updatePermission && scope.row.status != '发布'"
          >
            编辑
          </el-button>
          <el-button
            link
            type="primary"
            @click="push(`/demandDetail/demandManagePage?id=${scope.row.id}&role=${role}`)"
            v-if="scope.$index == 0 && updatePermission && scope.row.status !== '发布'"
          >
            管理
          </el-button>
          <el-button
            link
            type="primary"
            @click="push(`/demandView/demandViewPage?id=${scope.row.id}`)"
            v-else
          >
            查看
          </el-button>
          <el-button
            link
            type="primary"
            @click="releaseVersion(scope.row.id)"
            v-if="scope.row.status == '新建' && updatePermission"
          >
            发布
          </el-button>
          <el-button link type="danger" @click="handleDelete(scope.row.id)" v-if="delPermission && scope.row.status != '发布'"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      v-model:limit="queryParams.pageSize"
      v-model:page="queryParams.pageNo"
      :total="total"
      @pagination="getList"
    />
    <!-- 表单弹窗：添加/修改 -->
    <UpdateForm ref="formRef" @success="getList" />
  </ContentWrap>
</template>
<script lang="ts" setup>
import * as DemandApi from '@/api/demand'
import UpdateForm from './UpdateForm.vue'
defineOptions({ name: 'DemandManage' })
import { useAppStore } from '@/store/modules/app'
const { push, currentRoute } = useRouter()
const appStore = useAppStore()
const projectId = computed(() => appStore.getSelectProject)
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const loading = ref(false) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([] as any) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  versionName: '',
  projectId: projectId.value
})
const queryFormRef = ref() // 搜索的表单
const role = ref('')
const addPermission = computed(() => ['admin', 'projectManager', 'developer'].includes(role.value))
const delPermission = computed(() => ['admin', 'projectManager'].includes(role.value))
const updatePermission = computed(() =>
  ['admin', 'projectManager', 'developer'].includes(role.value)
)

watch(
  () => projectId.value,
  () => {
    queryParams.projectId = projectId.value
    getList()
    getProjectMemberRole()
  }
)

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await DemandApi.getDemandList(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}
// 获取人员在项目中角色
const getProjectMemberRole = async () => {
  let data = await DemandApi.getProjectMemberRole({ projectId: projectId.value })
  role.value = data
}
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加修改操作 */
const formRef = ref()
const updateForm = (type: string, row?: object) => {
  formRef.value.open(type, row)
}

// 删除
const handleDelete = async (id) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await DemandApi.deleteDemand({ id })
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}
// 发布
const releaseVersion = async (id) => {
  try {
    await message.confirm('确认发布该版本？')
    await DemandApi.releaseDemand({ id })
    message.success(t('common.updateSuccess'))
    await getList()
  } catch {}
}
/** 初始化 **/
onMounted(() => {
  if (projectId.value) {
    queryParams.projectId = projectId.value
    getList()
    getProjectMemberRole()
  }
})
</script>
<style scoped lang="scss"></style>
